//渲染表格
//获取表头数据
var titleData = []
//获取表体
var dataList = []
//获取筛选条件
var queryData = []
var colsList = [];

//初始化
var initData = function (){
    addQueryData()
    //获取表头数据
    tableTitleList()
    //获取数据
    tableDataList()
    //开启复选框
    var titleList = [{checkbox:true}]
    titleList = titleList.concat(titleData)
    colsList.push(titleList)

    tableInfo(colsList,dataList)

    // var table = layui.table;
    // table.render({
    //     id: 'idTest',
    //     elem: '#demo',
    //     cols:  colsList,
    //     data: dataList
    // })
}

//添加表单数据
var tableInfo = function (colsList,dataList){
    var table = layui.table;
    table.render({
        id: 'idTest',
        elem: '#demo',
        cols:  colsList,
        page: true,
        data: dataList
    })

    //复选框事件
    table.on('checkbox(test)', function(obj){
        // console.log(obj); //当前行的一些常用操作集合
        // console.log(obj.checked); //当前是否选中状态
        // console.log(obj.data); //选中行的相关数据
        // console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one
        //获取缓存数据
        var idTest = localStorage.getItem("idTest")
        idTest = JSON.parse(idTest)
        var type = obj.type
        if(type === 'all'){
            //获取选中的行
            var checkStatus = table.checkStatus('idTest');
            var chekDatas = checkStatus.data
            if(idTest == null || idTest.length === 0){
                var chekDataList = [];
                idTest = chekDataList.concat(chekDatas);
                // idTest = chekDataList;
            }else{
                var idTest2 = idTest;
                chekDatas.forEach(ckItem => {
                    var iscz = false
                    idTest.forEach(item => {
                        if(ckItem.id === item.id){
                            iscz = true
                        }
                    })
                    if(iscz === false){
                        idTest2.push(ckItem)
                    }
                })
                // idTest = idTest.concat(chekDatas)
                idTest = idTest2
            }
        }else{
            if(idTest == null){
                var chekDataList = [];
                obj.xhId = 1
                chekDataList.push(obj.data)
                idTest = chekDataList
            }else{
                if(obj.checked){
                    //判断当前选中的数据是否已经存在
                    var dataMap = obj.data
                    var iscz = false
                    idTest.forEach(item => {
                        if(dataMap.id === item.id){
                            iscz = true
                        }
                    })
                    if(!iscz){
                        idTest.push(dataMap)
                    }
                }else{
                    var iscz = false
                    var newIdList = [];
                    var dataMap = obj.data
                    idTest.forEach((item,i) => {
                        if(dataMap.id === item.id){
                            idTest.splice(i,1)
                        }
                    })
                }
            }
        }
        console.log("获取到的idTest："+JSON.stringify(idTest))
        localStorage.setItem('idTest', JSON.stringify(idTest))
    });
}


//获取列表表头字段信息
var tableTitleList = function (){
    $.ajax({
        url: '/seeyon/rest/jssdk/getTitleList',
        type: "GET",
        processData: false,
        contentType: false,
        async:false,
        success: function (data) {
            titleData = data
        },
        error: function (e) {
            console.log("获取二级明细字段异常！！！！！！！！！")
            titleData = [];
        }
    });
}

//获取筛选字段信息
var queryField = function (){
    $.ajax({
        url: '/seeyon/rest/jssdk/getQueryField',
        type: "GET",
        processData: false,
        contentType: false,
        async:false,
        success: function (data) {
            queryData = data
        },
        error: function (e) {
            console.log("获取筛选字段异常！！！！！！！！！")
            queryData = [];
        }
    });
}

//获取查询数据
var tableDataList = function (){
    $.ajax({
        url: '/seeyon/rest/jssdk/getDataList',
        type: "POST",
        processData: false,
        contentType: "application/json;charset=UTF-8",
        async:false,
        success: function (data) {
            dataList = data
        },
        error: function (e) {
            console.log("获取二级明细字段异常！！！！！！！！！")
            dataList = [];
        }
    });
}

//添加筛选项
var addQueryData = function (){
    queryField()
    var queryCondition = $("#queryCondition")
    var input = '';
    for(var i = 0; i<queryData.length;i++){
        input += queryData[i].title+'：</label><input type="text" id="'+queryData[i].field+'" name="title"  placeholder="请输入'+queryData[i].title+'" class="queryInput">&nbsp;&nbsp;&nbsp;&nbsp;'
    }
    input += '<button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="clickQuery()">' +
        '  <i class="layui-icon layui-icon-search"></i>' +
        '</button>'
    queryCondition.append(input);
}


//点击查询获取数据
var clickQuery = function (){
    var queryDataMap = {};
    for(var i = 0; i<queryData.length;i++){
        var value = $("#"+queryData[i].field+"").val()
        queryDataMap[queryData[i].field] = value
    }

    $.ajax({
        url: '/seeyon/rest/jssdk/getDataList',
        type: "POST",
        data: JSON.stringify(queryDataMap),
        processData: false,
        contentType: "application/json;charset=UTF-8",
        async:false,
        success: function (data) {
            dataList = data
            tableInfo(colsList,dataList)
        },
        error: function (e) {
            console.log("获取二级明细字段异常！！！！！！！！！")
            dataList = [];
        }
    });
    console.log("点击查询获取数据："+queryDataMap)
}